package com.nexgo.payment.pack;

import com.nexgo.common.ByteUtils;
import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.util.MessageHelper;

/**
 * QPS免密卡BIN黑名单下载
 *
 * Created by xiaox on 2017/8/12.
 */
class QpsBinCDownBuilder extends DataBuilder {
    QpsBinCDownBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
    }

    @Override
    SendData form() {
        SendData sendData = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sendData.setMsgType(mTransData.getTransType().getMsgType());
        sendData.setHeader(MessageHelper.formMessageHeader(mRepository.getParamValue(PreferencesConstants.TPDU)));
        sendData.setTerminalId(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        sendData.setMerchantId(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        sendData.setField60(formField60());
        sendData.setField62(formField62());
        return sendData;
    }

    private String formField62() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format(SysConstant.LOCALE, "%03d", mTransData.getCounter()));
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }

    private String formField60() {
        StringBuilder sb = new StringBuilder();
        sb.append(mTransData.getTransType().getFuncCode());
        sb.append(mRepository.getParamValue(PreferencesConstants.BATCH_NUMBER));
        sb.append("398");
        return ByteUtils.byteArray2HexString(sb.toString().getBytes());
    }
}
